/*  
 * @ Author:  蛋炒贩炒蛋
 * @ Date:  2021年8月11日15:30:06
 * @ Des:  全局公共方法
*/ 

import store from '@/store';
import http from "@/common/http";
// 获取时间段
const getTimeBucket = (time) => {
	var date = "";
	if (time) {
		if(time.indexOf("-")!=-1){
			time = time.replace(/-/g, '/');
		}
		date = new Date(time);
	} else {
		date = new Date();
	}
	let YY = date.getFullYear();
	let timestamp = date.getTime();
	let MM = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() * 1 + 1) : date.getMonth() + 1;
	let DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	let hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
	let mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
	let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	let timeBucket = ""
	if (hh < 22) {
		timeBucket = '晚上'
		if (hh < 20) {
			timeBucket = '傍晚'
			if (hh < 18) {
				timeBucket = '下午'
				if (hh < 13) {
					timeBucket = '中午'
					if (hh < 12) {
						timeBucket = '上午'
						if (hh < 9) {
							timeBucket = '早上'
							if (hh < 5) {
								timeBucket = '凌晨'
							}

						}
					}
				}
			}
		}
	} else {
		timeBucket = '深夜'
	}
	let tempTime = {
		YY: YY,
		MM: MM,
		DD: DD,
		hh,
		mm,
		ss,
		time: YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss,
		timestamp: timestamp,
		timeBucket
	}
	return tempTime
};
// 随机数方法
const random = (min, max) => {
	let num = Math.random() * (max - min) + min;
	num = Math.ceil(num)
	return num;
};

// 打开地图
const toNavigate = (longitude, latitude) => {
	uni.openLocation({
		latitude: latitude * 1,
		longitude: longitude * 1
	})
};
// 权限过滤跳转
const jumpPageFilter = (url) => {
	url = url.indexOf("?") != -1 ? url.split("?")[0] : url;

	return new Promise((resolve, reject) => {
		if (store.state.pathLimit.length > 0 && store.state.userInfo.type != 'customer') {
			if (store.state.pathLimit.indexOf(url) != -1) {
				uni.showToast({
					title: "暂无权限",
					icon: "none"
				})
				resolve(false)
			} else {
				resolve(true)
			}
		} else {
			resolve(true)
		}
	})
};
// 秒钟转换为时间 时分秒
const secondToTime = (second) => {
	let obj = {
		lev: "h",
		h: "",
		m: "",
		s: "",
		time: ""
	};
	if (second < 60) {
		obj.time = "00" + ":" + second < 10 ? '0' + second : second;
		obj.lev = "s";
		obj.h = 0;
		obj.m = 0;
		obj.s = second < 10 ? '0' + second : second;
	} else if (second < 3600) {
		obj.time =
			`${second/60<10?'0'+Math.floor(second/60):Math.floor(second/60)}:${second-(60*Math.floor(second/60))<10?'0'+(second-(60*Math.floor(second/60))):second-(60*Math.floor(second/60))}`;
		obj.lev = "m";
		obj.h = 0;
		obj.m = second / 60 < 10 ? '0' + Math.floor(second / 60) : Math.floor(second / 60);
		obj.s = second - (60 * Math.floor(second / 60)) < 10 ? '0' + (second - (60 * Math.floor(second / 60))) :
			second - (60 * Math.floor(second / 60));
	} else {
		let h = `${second/60/60<10?'0'+Math.floor(second/60/60):Math.floor(second/60/60)}`;
		let m = Math.floor((second - (60 * 60 * h)) / 60) < 10 ? "0" + Math.floor((second - (60 * 60 * h)) / 60) :
			Math.floor((second - (60 * 60 * h)) / 60);
		let s = (second - ((60 * 60 * h) + (60 * m))) < 10 ? "0" + (second - ((60 * 60 * h) + (60 * m))) : (second -
			((60 * 60 * h) + (60 * m)));

		obj.time = h + ":" + m + ":" + s;
		obj.lev = "h";
		obj.h = h;
		obj.m = m;
		obj.s = s;
	}
	return obj
};
// 路径切割获取参数
const getParameters = (tempUrl) => {
	let url = "";
	let dataObj = {
		oldUrl: "",
		newUrl: "",
		data: [

		]
	};
	if (tempUrl) {
		url = tempUrl
	} else {
		let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
		url = routes[routes.length - 1].route //获取当前页面路由
	}
	dataObj.oldUrl = url;
	if (url.indexOf("?") != -1) {
		let tempArr = url.split('?');
		dataObj.newUrl = tempArr[0];
		dataObj.data = [];
		if (tempArr[1].indexOf("&") != -1) {
			tempArr[1].split("&").forEach((d, i) => {
				dataObj.data.push({
					name: d.split('=')[0],
					value: d.split('=')[1]
				})
			})

		} else {
			dataObj.data = [{
				name: tempArr[1].split('=')[0],
				value: tempArr[1].split('=')[1]
			}]
		}

		return dataObj


	} else {
		return dataObj
	}

};
// 获取时间跨度
const getTimeSpan = (time) => {
	if (!time) {
		return 0;
	}
	time = time.replace(/-/g, '/');
	let oldTime = new Date(time).getTime();
	// 当前时间戳
	let nowTime = new Date().getTime();
	let tempTime = Math.abs(nowTime - oldTime) + 0.1;
	let timeObj = {
		YY: 0, //年
		MM: 0, //月
		DD: 0, //日
		hh: 0, //时
		mm: 0, //分
		ss: 0, //秒
	};
	timeObj.YY = Math.round(tempTime / 1000 / 60 / 60 / 24 / 365);
	timeObj.MM = Math.round(tempTime / 1000 / 60 / 60 / 24 / 30);
	timeObj.DD = Math.round(tempTime / 1000 / 60 / 60 / 24);
	timeObj.hh = Math.round(tempTime / 1000 / 60 / 60);
	timeObj.mm = Math.round(tempTime / 1000 / 60);
	timeObj.ss = Math.round(tempTime / 1000);
	return {
		oldTime: oldTime,
		newTime: nowTime,
		...timeObj
	}
};


// 获取多久之前
const getHowLongBefore = time => {
	time = time.replace(/-/g, '/');
	var date = new Date(time);
	// 传过来的时间戳
	let oldTime = date.getTime();
	// 当前时间戳
	let newTime = new Date().getTime();
	let Temp = newTime - oldTime;
	Temp = Math.abs(Temp);
	let n = '';
	let s = Math.round(Temp / 1000);
	if (s >= 60) {
		let f = Math.round(s / 60)
		// console.log(f + "分钟前")
		n = f + "分钟前"
		if (f >= 60) {
			let h = Math.round(f / 60)
			// console.log(h + "小时前");
			n = h + "小时前"
			if (h >= 24) {
				let day = Math.round(h / 24);
				// console.log(day+"天前");
				n = day + "天前";
				if (day >= 30) {
					let m = Math.round(day / 30);
					// console.log(m+"月前");
					n = m + "月前"
					if (m >= 12) {
						let y = Math.round(m / 12)
						// console.log(y+"年前");
						n = y + "年前"
					}
				}
			}
		}
	} else {
		if (s < 0) {
			// console.log(s + "秒钟前");
			n = "刚刚"
		} else {
			n = s + "秒前"
		}
	}
	return n;
};
// 获取两个时间差
const getTimeDifference = (time1, time2) => {
	if (!time1 || !time2) {
		return
	}
	// 判断是时间戳出还是格式化时间
	// 转换time1
	if (time1 != parseInt(time1)) {
		// 是格式化的时间
		time1 = time1.replace(/-/g, '/'); //兼容ios
		time1 = new Date(time1).getTime()
	}
	// 转换time2
	if (time2 != parseInt(time2)) {
		// 是格式化的时间
		time2 = time2.replace(/-/g, '/'); //兼容ios
		time2 = new Date(time2).getTime()
	}
};
// 获取地址
const getLocation = (obj) => {
	return new Promise((resolve, reject) => {
		let config = {
			// 更新保存的当前位置
			updateLocation: obj && obj.updateLocation ? true : false
		}
		uni.getLocation({
			type: "gcj02",
			geocode: true,
			success: (res) => {
				console.log(res)
				let tempLocation = {
					latitude: res.latitude, //维度
					longitude: res.longitude, //经度
					province: res.address.province, //省份,
					city: res.address.city, //城市
					district: res.address.district,
					street: res.address.street,
					streetNum: res.address.streetNum,
					poiName: res.address.poiName,
					cityCode: res.address.cityCode,
				}
				if (config.updateLocation || !store.locationInfo) {
					store.commit("setLocationInfo", tempLocation)
				}

				resolve(tempLocation)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

module.exports = {
	jumpPageFilter: jumpPageFilter,
	getTimeBucket: getTimeBucket,
	getHowLongBefore: getHowLongBefore,
	getParameters: getParameters,
	getLocation: getLocation,
	toNavigate: toNavigate,
	secondToTime: secondToTime,
	getTimeSpan: getTimeSpan,
	getTimeDifference: getTimeDifference,
	random:random
}